package fr.noe.amicaleloisirs.concours.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.noe.amicaleloisirs.concours.constants.Constants;
import fr.noe.amicaleloisirs.concours.utils.FileUtil;

/**
 * Servlet implementation class ChargerConcours
 */
public class ChargerConcours extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChargerConcours() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String nomFichier = request.getParameter("dateConcours");
		if (nomFichier == null)
			response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Le nom du fichier n'est pas indiqué");
		else {
			String path = Constants.CONCOURS_URL_FILES + nomFichier + ".xml";
			if (FileUtil.fileExists(path)) {
				response.setContentType("text/xml;charset=UTF-8");
				PrintWriter out = response.getWriter();
				out.write(FileUtil.read(path));
			}
		}
	}

}
